<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="customerName" label="评论人">
          <el-input
            v-model="queryParams.customerName"
            placeholder="评论人"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="businessQuery"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="commentsList"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          label="评论人"
          prop="customerName"
          width="150"
          align="center"
        />
        <el-table-column
          label="评论内容"
          prop="commentContent"
          min-width="200"
          align="center"
        />
        <el-table-column
          label="评分（1-5星）"
          prop="rating"
          width="200"
          align="center"
        />
        <el-table-column
          label="评论时间"
          prop="createdTime"
          width="300"
          align="center"
        />
        <el-table-column fixed="right" label="操作" width="220">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="handleDelete(scope.row.id)"
            >
              <i-ep-delete />删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { getCommentPage, deleteComment } from "@/api/comments/index";
import { CommentQuery, CommentPageVO } from "@/api/comments/type";

defineOptions({
  name: "Comment",
  inheritAttrs: false,
});

const dateTimeRange = ref(undefined);
const queryFormRef = ref(ElForm);

const loading = ref(false);
const ids = ref<number[]>([]);
const total = ref(0);

const queryParams = reactive<CommentQuery>({
  pageNum: 1,
  pageSize: 10,
});

const commentsList = ref<CommentPageVO[]>();

/** 查询 */
function handleQuery() {
  loading.value = true;
  getCommentPage(queryParams)
    .then(({ data }) => {
      commentsList.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}
/** 重置查询 */
function businessQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  dateTimeRange.value = undefined;
  handleQuery();
}

/** 行checkbox 选中事件 */
function handleSelectionChange(selection: any) {
  ids.value = selection.map((item: any) => item.id);
}

/** 删除角色 */
function handleDelete(roleId?: number) {
  const roleIds = [roleId || ids.value].join(",");
  if (!roleIds) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    loading.value = true;
    deleteComment(roleIds)
      .then(() => {
        ElMessage.success("删除成功");
        businessQuery();
      })
      .finally(() => (loading.value = false));
  });
}
onMounted(() => {
  handleQuery();
});
</script>
